﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using QLKDS.DTO;

namespace QLKDS.DAL
{
    public class ReligiousManager
    {
        public ReligiousInfo GetReligiousById(int id)
        {
            using (var db = new QLKDSDataContext())
            {
                var queryReligious = from d in db.Religious
                                    where d.Id.Equals(id)
                                    select new ReligiousInfo { Id = d.Id,  Name = d.Name,Description=d.Description};

                return queryReligious.First();
            }
        
        }


        public IList<ReligiousInfo> ListReligious()
        {
            using (var db = new QLKDSDataContext())
            {
                var queryReligious = from d in db.Religious
                                   select new ReligiousInfo { Id = d.Id,Name = d.Name,Description=d.Description };

                return queryReligious.ToList();
            }
        }
    

        public void CreateReligious(Religious Religious)
        {
            using (var db = new QLKDSDataContext())
            {
                db.Religious.InsertOnSubmit(Religious);

                db.SubmitChanges();
               
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="Religious"></param>
        public void UpdateReligious(Religious Religious)
        {
            using (var db = new QLKDSDataContext())
            {
                var _Religious = db.Religious.Single(d => d.Id.Equals(Religious.Id));

                _Religious.Name = Religious.Name;
                _Religious.Description = Religious.Description;
                db.SubmitChanges();
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="id"></param>
        public void DeleteReligious(int id)
        {
            using (var db = new QLKDSDataContext())
            {
                var _Religious = db.Religious.Single(d => d.Id.Equals(id));

                db.Religious.DeleteOnSubmit(_Religious);
                db.SubmitChanges();
            }
        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public bool HasNameExisted(string name)
        {
            using (var db = new QLKDSDataContext())
            {
                var list = (from d in db.Religious
                            where d.Name.Equals(name)
                            select d.Id).ToList();

                return list.Count > 0;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="code"></param>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool HasNameDuplicated(string name, long id)
        {
            using (var db = new QLKDSDataContext())
            {
                var list = (from d in db.Religious
                            where d.Name.Equals(name)
                            where !d.Id.Equals(id)
                            select d.Id).ToList();

                return list.Count > 0;
            }
        }

     
    }
}
